package com.jash.porject;

import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ClickableSpan;
import android.text.style.ImageSpan;
import android.text.style.RelativeSizeSpan;
import android.text.style.StrikethroughSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String str = "你好[zuohengheng],adfasdkfj[xu]";
        SpannableString txt = new SpannableString(str);
        Pattern pattern = Pattern.compile("\\[(.+?)\\]");
        Matcher matcher = pattern.matcher(str);
        AssetManager assets = getAssets();
        AnimationDrawable drawable = new AnimationDrawable();
        try {
            String[] list = assets.list("");
            for (String s : list) {
                if (s.startsWith("d_")) {
                    Bitmap bitmap = BitmapFactory.decodeStream(assets.open(s));
                    drawable.addFrame(new BitmapDrawable(getResources(), bitmap), 500);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        while (matcher.find()) {
            try {
                Bitmap bitmap = BitmapFactory.decodeStream(assets.open("d_" + matcher.group(1) + ".png"));
                txt.setSpan(new ImageSpan(this, bitmap), matcher.start(), matcher.end(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        txt.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                Toast.makeText(MainActivity.this, "你好", Toast.LENGTH_SHORT).show();
            }
        }, 0, 2, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        TextView text = (TextView) findViewById(R.id.main_text);
        text.setText(txt);
//        ImageView imageView = (ImageView) findViewById(R.id.main_image);
//        imageView.setImageDrawable(new NetDrawable());
//        ((NetDrawable) imageView.getDrawable()).loadImage("");
    }
}
